package boardgamer.common.client.preference;

import android.content.SharedPreferences;
import android.preference.EditTextPreference;

import static com.google.common.base.Preconditions.checkNotNull;

/**
* @author garysoed
*/
public class RegisteredEditText<V> extends
    AbstractRegisteredPreference<V, EditTextPreference> {

  private final StringConverter<V> stringConverter;

  RegisteredEditText(String key, EditTextPreference preference, V defaultValue,
      SharedPreferences sharedPreferences, StringConverter<V> stringConverter) {
    super(key, preference, defaultValue, sharedPreferences);
    this.stringConverter = checkNotNull(stringConverter, "stringConverter");
  }

  @Override
  protected V getFromSharedPreferences() {
    return stringConverter.valueOf(
        sharedPreferences.getString(key, stringConverter.toString(defaultValue)));
  }

  @Override
  public void setValue(V newValue) {
    preference.setSummary(newValue.toString());
  }
}
